package com.woniu.supermarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Brand;
import com.woniu.supermarket.entity.Category;
import com.woniu.supermarket.mapper.BrandMapper;
import com.woniu.supermarket.mapper.CategoryMapper;
import com.woniu.supermarket.service.IBrandService;
import com.woniu.supermarket.util.CodeCreateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class BrandServiceImpl implements IBrandService {

    @Autowired
    BrandMapper brandMapper;


    /**
     * 分页查询品牌
     * @param now 当前页
     * @param size 当前页大小
     * @param cons 查询条件 ：品牌名或品牌编码
     * @return
     */
    @Override
    public PageInfo<Brand> queryBrandByPage(Integer now, Integer size, String cons) {
        PageHelper.startPage(now,size);
        List<Brand> list = brandMapper.selectBrandByCons(cons);
        return new PageInfo<>(list);
    }

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @Override
    public Brand queryBrandById(int id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加品牌
     * @param brand
     * @return
     */
    @Transactional
    @Override
    public int addBrand(Brand brand) {
        brandMapper.insert(brand);
        Integer id = brand.getId();
        brand.setBrandCode(CodeCreateUtil.createGoodsBrandCode(id));
        return brandMapper.updateByPrimaryKey(brand);

    }

    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @Override
    public int modifyBrand(Brand brand) {
        return brandMapper.updateByPrimaryKey(brand);
    }

    /**
     * 根据id删除品牌
     * @param id
     * @return
     */
    @Override
    public int removeBrandById(int id) {
        return brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查找所有品牌
     * @return
     */
    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }
}
